/**************************************************************************
 * @doc PRINT
 * @module print.c |
 * Source code for handling the printer functionality
 *
 * Product     :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :
 * <nl>Notes       :
 *
 * @head3 Printer Functions |
 * @index | Print
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 *
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form
 * or by any means whether electronic, mechanical, magnetic, optical,
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 $Log: /SoftPay/SPK2SIM/Source/Print/print.c $
 * ------------------------------------------------------------------------
 ********************************************************************************/
#include <project.h>
#define VSPRINT_C

#include <aclconio.h>
#include <txoinc.h>
#include <ascii.h>
#include <errno.h>
#include <applidl.h>
#include <printer.h>
#include <formater.h>
#include <message.h>
#include <string.h>
#include <acldev.h>
#include <battery.h>

/** /#define DEBUG_VSPRINT*/
#ifdef DEBUG_VSPRINT
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <print.h>
#include <gds.h>
#include <lct.h>
#include <tblutil.h>
#include <tct.h>
#include <pct.h>
#include <ui.h>
#include <msg.h>
#include <msgfuncs.h>
#include <form.h>
#include <stats.h>
#ifdef PARAMRPT_C
#include <paramrpt.h>
#endif  /* PARAMRPT_C */
#include "hostform.h"
#include "applic.h"
#include <spdebug.h>
#include <init.h>
#include <util.h>
#include <confio.h>
#include <oper.h>
#include <entry.h>
#include <file.h>
#include <logo.h>

char sz_FS[] = "\034"; /* String FS */

PRINTER_OBJECT obPrinter;
static FORMATER srFormater;

extern VS_BOOL fSettmentFlag;  //jrd spqalogo44 spqalog024

int inInitPrintandForm(TRANSACTION_OBJECT* pobTran) {
    char szLCTTemplateFileName[FILENAME_SIZE + 1];
    int inHandleLogoFile = -1;
    int retval = -1;

    // JTB This sets up the ACT2000 formater engine with a pointer to gvardata
    //     This is needed when using ACT2000 as a shared library
    formater_main(gvardata);

    vdDisplayMessageFromFile(PRINTER_CHECK_MSG21, STATUS_MSG);
    inOpenPrinter();

    /* Added by Sadiq_m1 for Logo Download - Modified for one time download */

    if (shDnldGraphicFns[obPrinter.inType] != NULL_CH) {
        inHandleLogoFile = open("RcptLogo.lgo", O_RDONLY);
        LOG_PRINTFF((0X08L, "IN PRINTER Handle %d", inHandleLogoFile));
        if (inHandleLogoFile > 0) /* File Opened Successfully */ {
            LOG_PRINTFF((0X08L, "IN PRINTER Type   %d", obPrinter.inType));
            //retval = p3300_dnld_graphic_file (obPrinter.inHandle, inHandleLogoFile);
            retval = shDnldGraphicFns[obPrinter.inType] (obPrinter.inHandle, inHandleLogoFile);
            SVC_WAIT(2000);
            LOG_PRINTFF((0X08L, "IN PRINTER download  %d", retval));
            if (retval < 0) {
                vdDisplayAt(1, 1, "Download Logo Failed", CLR_EOL);

                vdSGErrorMessage(PRINTER_ERR_MSG21);
                INCREMENT_PRINTER_ERROR;
            } else {
#ifdef _TARG_68000
                inRemoveFile("RcptLogo.lgo"); /* Remove the file to avoid multiple downloads at each reboot. */
#endif /* _TARG_68000 */
            }
        }
    }

    //  inPrintLines(&obPrinter, 1,1,0,0);
    // p3700_print_graphic(obPrinter.inHandle,0,80);
    /* Do not set template if NOPRINTER */
    if (inGetPrinterType() != NOPRINTER) {
        inSelectTemplateFile(&obPrinter);
        inGetDefaultTemplateName(szLCTTemplateFileName);
        if (inOpenFormater(szLCTTemplateFileName, &obPrinter)) {
            vdSGErrorMessage(FORMATER_ERR_MSG);
            return (VS_ERR);
        }
    }

    SVC_WAIT(1300);
    return (VS_SUCCESS);
}

short default_init(short hPrinter,short shTimeout) {
    return(OK);
}

short default_print(short hPrinter,unsigned char* pchTemp) {
    return(OK);
}

short default_close(short hPrinter) {
    return(OK);
}


void vdFormFeed(PRINTER_OBJECT* obPrinter) {
    pdebug(("--vdFormFeed--"));
    obPrinter->shPrint(obPrinter, "\f");
}


short (*shInitFns[])(short,short) =
{
default_init, /* default init */
p950_init,    /* P950    ,,  */
p3700_init,   /* P3700    ,,  */
p3600_init,   /* P3600    ,,  */
p3300_init,   /* P3300    ,,  */
};

short (*shIdFns[])(short,short) =
{
default_init, /* default init */
p950_id,      /* P950   ,,   */
p3700_id,     /* P3700   ,,   */
p3600_id,     /* P3600   ,,   */
p3300_id,     /* P3300   ,,   */
};

#define NUMBER_OF_INSTALLED_PRINTERS    sizeof(shInitFns) / sizeof(short *)


short (*shPrintFns[])(short,unsigned char*) =
{
default_print, /* default print  */
p950_print,   /* P950     ,,  */
p3700_print,  /* P3700     ,,  */
p3600_print,  /* P3600     ,,  */
p3300_print,  /* P3300     ,,  */
};

short (*shStatusFns[])(short,short) =
{
default_init, /* default status */
p950_status,  /* P950     ,,   */
p3700_status, /* P3700     ,,   */
p3600_status, /* P3600     ,,   */
p3300_status, /* P3300     ,,   */
};

short (*shCloseFns[])(short) =
{
default_close, /* default close */
p950_close,   /* P950     ,,  */
p3700_close,  /* P3700     ,,  */
p3600_close,  /* P3600     ,,  */
p3300_close,  /* P3300     ,,  */
};

short (*shDnldGraphicFns[])(short,short) =
{
default_init,               /* default download graphic file */
NULL_CH,        /* P950     ,,  */
p3700_dnld_graphic_file,    /* P3700     ,,  */
p3600_dnld_graphic_file,    /* P3600     ,,  */
p3300_dnld_graphic_file,    /* P3300     ,,  */
};

PRINTER_PROPERTIES srPtrProps[] = {
    { NOPRINTER, VS_FALSE },                /* default */
    { COLUMN_TYPE_MULTIPLE, VS_TRUE },      /* P950 */
    { COLUMN_TYPE_MULTIPLE, VS_TRUE },      /* P3700 */
    { COLUMN_TYPE_MULTIPLE, VS_TRUE },      /* P3600 */
    { COLUMN_TYPE_MULTIPLE, VS_TRUE },      /* P33XX */
};

/*-------------------------------------------------------------------------
    Function : spool()
    Description : spools data onto a global buffer spool_buf;
                  initiates actual write to device if spool buf is full;
                  this is done to make use of the comm buffers efficiently!

    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1       ?        ?        Creation.
      2    4/28/95  Eddy_B1     Moved over from vsisoprn.c
--------------------------------------------------------------------------*/

short shSpool(PRINTER_OBJECT* obPrinter,char* szBuffer) {
    char buffer[4 + 1];

    pdebug(("--shSpool--"));

    /* Wait till entire buffer is printed fully */
    while (get_port_status(obPrinter->inHandle, buffer) != 0);

    return(shPrintFns[obPrinter->inType]((short) obPrinter->inHandle, (unsigned char *) szBuffer));
}

/*-------------------------------------------------------------------------
    Function :    inSelCharSetFn()
    Description : Selects the correct printer command for the corresponding
                  printer, depending on the country code specified in the
                  vslct.txt file.  Writes command escape seq. to printer handle.
    Parameters :  The obPrinter object in vsprint.h
    Returns :     VS_ERR if printer not supported.  WRITE command return
                  value if printer is supported.
    Globals :
    Notes :      The printer command is: (ESC h n ;) in ASCII.  or in hex:
                 (0X1B 0X68 0X3n 0X3B), where n is the country code from
                 0 to 9(30h to 39h).
                 This command is the same for 250, 600, 700 and 900 printers.
                 Check the printer manual for any additional printers.
                 Additional code will be needed to build the szCSetBuffer
                 if the escape sequence is different for any additional
                 printers.

    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   12/20/95  Gus_T1      Creation.

--------------------------------------------------------------------------*/

int inSelCharSetFn(PRINTER_OBJECT* obPrinter) {
    char chCtryCodeSeq = '0';
    static char szCSetBuffer[4 + 1];

    /** /pdebug(("--inSelCharSetFn--"));*/

    /* Get the country code and add it to '0'.  This builds
     * the 3rd variable in the select character set printer command.
     */
    chCtryCodeSeq = (char) ((int) chCtryCodeSeq + inGetCountryCode());
    /** /pdebug(("Country seq. code is %c.", chCtryCodeSeq));*/

    /* Build the buffer with the command to be sent to the printer. */
    sprintf(szCSetBuffer, "%c%c%c;", 0x1B, 0x68, chCtryCodeSeq);
    /** /pdebug(("Char. buffer = %s", szCSetBuffer));*/

    /*  Write the command sequence to the handle  */
    return(write(obPrinter->inHandle, szCSetBuffer, strlen(szCSetBuffer)));
}


void vdInitializePrinter(PRINTER_OBJECT* obPrinter) {
    /** /pdebug(("--vdInitializePrinter--"));*/

    obPrinter->shInit = shInitFns[obPrinter->inType];
    obPrinter->inSelCharSet = (PFI_VD) inSelCharSetFn;
    obPrinter->shPrint = (PFI_VOID_CHAR) shSpool;
    obPrinter->shStatus = shStatusFns[obPrinter->inType];
    obPrinter->shClose = shCloseFns[obPrinter->inType];
    obPrinter->vdFormFeed = (PFV_VD) vdFormFeed;
}

/*-------------------------------------------------------------------------
    Function :     inSetupForm
    Description :
              - Initialises the requested printer.
              - Fills up the FORMTER struct members, for future use.
              - Verifies that the requested printer is connected to the
                terminal.
    Parameters :  psrFormater  - The pointer to FORMATER struct.
                  obPrinter->inType - Printer being used (one of PRINTER_150,
                                  PRINTER_250, PRINTER_600 & PRINTER_700).
    Returns :    0 on success.
                 No other return values can be expected.
    Globals :
    Notes :      This function is called by formater_open(), as a part of
                 formatter initialisation. formater_open() expects a "0" as
                 a return value from this function on success. This function
                 is generally not used other than by formater_open().
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   06/22/95  Henry_G1   Replaced inGetChar with getchar.
      2   06/28/95  Henry_g1   Replaced InitFns call with StatusFns.
      3   06/30/95  Henry_G1   Added wait for output pending loop.
      4   07/10/95  Henry_G1   Now detecting a paper low condition.
     5   06/17/96  Monique_B1 Removed ioctl loop, it was endless if
                              printer was disconnected.
     6   08/06/96  Monique_B1 Removed Paper low message
--------------------------------------------------------------------------*/
typedef short  (*SFP_SHORT)(int);

int inSetupForm(FORMATER* psrFormater,int inTimeOut) {
    int status,
    inThreeTimes = 1;
    char pchIoctrlBuffer[IOCTRL_BUFFER_SIZE];
    PRINTER_OBJECT* pobPrinter;

    pobPrinter = &obPrinter;
    /** /pdebug(("--inSetupForm--"));*/
    /** /pdebug(("pobPrinter->inType = %d", pobPrinter->inType));*/

    psrFormater->direct = 1;      /* Direct printing, no spooling  */
    psrFormater->max_buffers = 2;      /* Max no. of buffers to be used */

    psrFormater->output_close = pobPrinter->shClose;
    psrFormater->output_print = shPrintFns[pobPrinter->inType];
    psrFormater->output_mode = pobPrinter->inMode;

    /* Wait for pending output before checking printer status */

    do {
        status = get_port_status(pobPrinter->inHandle, pchIoctrlBuffer);
        if (status) {
            pause(100); /* wait 1 second */
            inThreeTimes++;
        }
    } while ((inThreeTimes != 3) && (status == 1));

    /* The following loop does nothing! since status is set to OK */
    do {
        //NKJT Simulator Error -- Simulator does not return the Printer Status
        // status = pobPrinter->inStatus(pobPrinter->inHandle, PRINTER_TIME_OUT);
        status = OK;
        /** /pdebug(("status = %d", status));*/

        if ((status != OK) && (status != PAPER_LOW)) {
            /* Resetting the printer driver for the next cycle */
            close(pobPrinter->inHandle);
            inOpenPrinter();

            if (inThreeTimes == 3) {

                vdSGErrorMessage(PRINTER_ERR_MSG21);
                INCREMENT_PRINTER_ERROR;
                return(VS_ERR);
            } else
                inThreeTimes++;
        }
    } while ((status != OK) && (status != PAPER_LOW));

    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :     vdGetPrinterName
    Description :
              - Get Printer name
    Parameters :  szPathName, inComIndex
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   12/10/96  Page_K1   Created
--------------------------------------------------------------------------*/
void vdGetPrinterName(char* szPathName,int inComIndex) {
    sprintf(szPathName, "%s%d", "/dev/com", inComIndex);
}

/*-------------------------------------------------------------------------
    Function :     inOpenPrinter
    Description :
              - Opens the printer.
              - Sets the PrinterType, EnablePrinter flag and CommIndex
    Parameters :
    Returns :    0 on success.
                 VE_ERR on error.
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   12/10/96  Page_K1    Set EnablePrinter flag and CommIndex.
      2   01/06/97  Page_K1    Check printer ID and only call init function
                                for printer that returns corresponding ID
      3   01/17/97  Page_K1    Do not display NO PRINTER if printer disabled
      4   12/17/97  Page_K1    Add confirm if NO PRINTER.
      5   12/17/97  Page_K1    Redisplay "Printer Check..." after confirm
      6   12/19/97  Page_K1    Fix bug in "Printer Check..."
      7   01/08/98  Page_K1    Do not limit loop on printer check confirm
      8   02/20/98  KC_C1      If time-out occured inside in ChooseOne
                               gu_clr_state will be set to one.
                               Need to set gu_clr_state back to zero.
--------------------------------------------------------------------------*/

int inOpenPrinter() {
    VS_BOOL fPrinterFound;
    signed char chAnswer;

    int inComIndex;
    short shIdPrinterResult;
    short shInitPrinterResult;
    char szLocBuf[3 + 1];
    char szPathName[10];
    VS_BOOL fForwards = VS_FALSE;
    char* chNull = 0;


    /**/pdebug(("--inOpenPrinter--"));/**/
 #if 1 //ndef IMAM_TUNE_DBS
    inComIndex = (inGetCommIndex() == 0 ? 1 : inGetCommIndex());
    if (inComIndex == 1)
        fForwards = VS_TRUE;

    strcpy(szLocBuf, "8N1");

    vdSetPrinterType(NOPRINTER);
    fPrinterFound = VS_FALSE;

    // If they turned the printer off via PCT Enable Printer then later
    // they might want to turn it back on without rebooting so initialize
    // always so the printer is ready in the background.
    /*
    if (!fGetEnablePrinter())
    {
        obPrinter.inType = 0;
        if (inSavePCTRec( 0 ) != VS_SUCCESS)
        {
            return(VS_ERR);
        }
        return(VS_SUCCESS);
    }
    */

    while (VS_TRUE) {
        /* Search printer ports for successful initialization */
        do {
            vdGetPrinterName(szPathName, inComIndex);

            pdebug(("szPathName = %s", szPathName));
            pdebug(("inComIndex = %d", inComIndex));
            /* Open printer and set baud */
            if (((obPrinter.inHandle = open(szPathName, 0)) >= 0) && (p_set_baudformat(obPrinter.inHandle, 19200, szLocBuf) >= 0)) {
                pdebug(("Printer Handle: %d", obPrinter.inHandle));
                /* Send <FS> and <ESC><x> to printer to put in native mode for checking ID*/
                // NKJT Simulator Error -- Simulator Does not allow characters to be sent
                //      to the printer before initialisation.
                // write(obPrinter.inHandle, sz_FS, 1);
                // write(obPrinter.inHandle, "\033\170", 2);
                /* Iterate through the different printer types until successful initialization */
                for (obPrinter.inType = (NUMBER_OF_INSTALLED_PRINTERS - 1); obPrinter.inType > 0; obPrinter.inType--) {
                    /* Get printer ID using id function for this printer type  */
                    shIdPrinterResult = shIdFns[obPrinter.inType](obPrinter.inHandle, PRINTER_TIME_OUT);
                    pdebug(("Printer ID: %d", obPrinter.inType));
                    pdebug(("Printer ID Result: %d", shIdPrinterResult));
                    if (shIdPrinterResult == OK) {
                        /* Initialize printer object function pointers (i.e  obPrinter->shFunc = shFuncArr[type] ) */
                        vdInitializePrinter(&obPrinter);
                        /* Call the corresponding printer type initialization routine */
                        shInitPrinterResult = obPrinter.shInit(obPrinter.inHandle, PRINTER_TIME_OUT);
                        pdebug(("Printer Init Result: %d", shInitPrinterResult));
                        if (shInitPrinterResult == OK) {
                            /* Store current printer object properties in PCT structure */
                            vdSetPrinterType((char) (obPrinter.inType));
                            vdSetCommIndex((char) inComIndex);
                            if (inSavePCTRec(0) != VS_SUCCESS)
                                return(VS_ERR);
                            return(VS_SUCCESS);
                        }

                        if (shInitPrinterResult == BAD_ID)
                            fPrinterFound = VS_TRUE;
                    }
                }
            }
            if (!fPrinterFound) {
                close(obPrinter.inHandle);
                if (fForwards)
                    inComIndex++;
                else
                    inComIndex--;
            }
        } while ((inComIndex <= MAX_COMM_PORTS) && (inComIndex > 0) && !fPrinterFound);

        /* Tell user about missing printer if not found */
        if (!fPrinterFound) {
			//break;
            clrscr();
			break;
            vdSetActionFont();
            chAnswer = (char) obDisp.inDO_ChooseOne(chNull, pchGetMsg(PRNTR_CONNECTED_MSG15), pchGetMsg(YESNO_MSG5x), 2, NULL_PFI);
            vdDisplayMessageFromFile(PRINTER_CHECK_MSG21, STATUS_MSG);
            vdSetMenuFont();
            if (chAnswer == KEY_YES)
                inComIndex = 1;
            else
                break;
        }
        /* Found printer, exit while (TRUE) to continue */
        else
            break;
    }


    /*
     *      Save status to PCT
     */
    if (inSavePCTRec(0) != VS_SUCCESS) {
        close(obPrinter.inHandle);
        return(VS_ERR);
    }
#if 0
    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter())
        vdDisplayMessageFromFile(NO_PRINTER_MSG, WARNING_MSG);
#endif
    return(VS_ERR);
}
#endif

int inChkPrinterStatus(PRINTER_OBJECT* pobPrinter) {
    int inResult;
    int inPriorStatus = YES;
	#ifdef IMAM_REMOVE
	 return(VS_SUCCESS);

	#endif

    //pdebug(("--inChkPrinterStatus--"));

    /*
     *      Printer is NOT available or NOT Enabled.
     */
    if (inGetPrinterType() == NOPRINTER)// || fGetEnablePrinter() == NO)
        return(VS_SUCCESS);

    /*
     *      Check the Printer Status
     */
    inResult = pobPrinter->shStatus(pobPrinter->inHandle, PRINTER_TIME_OUT);
    //pdebug(("  Stat=%d", inResult));
    switch (inResult) {
    case PAPER_OUT:
        inPriorStatus = fGetEnablePrinter();
            INCREMENT_PRINTER_ERROR;
        inResult = VS_ERR;
        /* vdSetPCTEnablePrinter(NO); */
        break;
    case OK:
    case PAPER_LOW:
        //vdSetPCTEnablePrinter(YES);
        inPriorStatus = NO;
        inResult = VS_SUCCESS;
        break;
    default:
        /*
        *      Attempt to re-open the Printer, this will set printer to NO_PRINTER on failure.
        */
        LOG_PRINTF(("Stat=%d,errno=%d", inResult, errno));
        close(pobPrinter->inHandle);
        inResult = inOpenPrinter();
        if (inResult == OK)
            inPriorStatus = NO;
        break;
    }

    if (inPriorStatus) {

        vdDisplayMessageFromFile(PRINTER_ERR_MSG21, WARNING_MSG);
        /* Already done above - mdm - 07/21/00 */
        /* INCREMENT_PRINTER_ERROR; */
    }


    return (inResult);

}


/*-------------------------------------------------------------------------
    Function :    inPrintLines
    Description :
        Prints the requested lines.
             - Validates that line-no.s are valid.
             - Attempts to print the requested lines.
    Parameters :
        inFromLine   - Print from this line no. The line no.s are
                        referred with respect to the template file.
        inToLine     - Print upto this line.
    Returns :
        1  on success.
        0  on error. The global "errno" variable is copied with one
                             of the following error codes.
        E_INVALID_LINES, if inFromLine > inToLine.
    Globals :
    Notes :
        This function is just responsible for printing the requested
        lines. The variables constituting these lines should have
        copied with their values. As long as proper line no.s are
        sent when this function is called, it is not necessary to
        check the return value of this function.
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   05/28/95  Henry_G1   Added lnCondition parameter.
      2   07/11/95  Henry_G1   Added obPrinter->inType parameter.
      3   10/03/96  Page_K1    Move check on pending output before status check
      4   10/11/96  Page_K1    Remove check on pending output and printer status
      --------------------------------------------------------------------------*/

int inPrintLines(PRINTER_OBJECT* obPrinter,int inFromLine,int inToLine,int inBlLine,unsigned long lnCondition) {
    int inErrLine = 0;             /* Line-no, where printing failed   */
    int i = 0;
    /** /pdebug(("--inPrintLines--"));*/



    if (inFromLine > inToLine)
        return(VS_ERR);

    /* Instruct the formatter to print those requested     */
    /* lines. If any errors in printing a particular line, */
    /*  re-start from that line, in the next iteration.    */
//jrd spqalog24
 if (fSettmentFlag == 1)
 {
 //do nothing
 }
 else
 {
     if (inChkPrinterStatus(obPrinter) != VS_SUCCESS)
     {  // Error message and record for stats

        return(VS_ERR);
 	 }
 }
    if (inGetPrinterType() != NOPRINTER && fGetEnablePrinter()) {
        /*
         *      Set Timer to abort attempt if printer no longer responding.
         */
        //This has to be resolved .....
        while ((i = formater_line(&srFormater, inFromLine, inToLine, inBlLine, lnCondition, &inErrLine)) != 0) {
            //inChkPrinterStatus(obPrinter);  // Error message and record for stats
            inFromLine = inErrLine;
        }
    }

    return(VS_SUCCESS);
}

int inOpenFormater(char* szFormName,PRINTER_OBJECT* pobPrinter) {
    int inStatus = VS_SUCCESS;

    /** /pdebug(("--inOpenFormater--"));*/

    if ((pobPrinter->inSelCharSet(pobPrinter)) < 0) /* Select Char Set , Gus_T1 */ {
        /** /pdebug(("Char set selection failure"));*/
        inStatus = VS_ERR;
    }

    if (inStatus != VS_ERR) {
        /*extern int formater_open(int, FORMATER *, char *, int (*)(FORMATER *, int), int); */
        /* ========== CBS This line will probably have to be modified to work under Visual C ========== */
        if (formater_open(pobPrinter->inHandle, &srFormater, szFormName, inSetupForm, 30/*pobPrinter*/) < 0) {
            errno = E_INVALID_PRINTER;
            inStatus = VS_ERR;
        }
    }

    if (inStatus != VS_ERR)
        return (VS_SUCCESS);

    close(pobPrinter->inHandle);
    vdDisplayMessageFromFile(CONNECT_PRINTER_MSG, STATUS_MSG);

    vdDisplayMessageFromFile(PRINTER_CHECK_MSG21, STATUS_MSG);

    if ((inStatus = inOpenPrinter()) == VS_ERR) {
        vdDisplayMessageFromFile(PRINTER_ERR_MSG21, WARNING_MSG);
        INCREMENT_PRINTER_ERROR;
        pause(100);
        return(VS_ERR);
    }

    return(VS_SUCCESS);
}

int inCloseFormater() {
    /** /pdebug(("--inCloseFormater--"));*/

    formater_close(&srFormater);
    return(VS_SUCCESS);
}

int inSwitchTemplate(char* szTemplateName) {
    int inStatus = VS_SUCCESS;

    /** /pdebug(("--inSwitchTemplate--"));*/

    if (formater_template(&srFormater, szTemplateName) < 0) {
        errno = E_NO_TEMPLATE_FL;
        inStatus = VS_ERR;
    }

    if (inStatus == VS_ERR)
        return(errno);

    return (VS_SUCCESS);
}

void p350ColumnMode(int mode) {
    char szPrinterCommand[6];

    switch (mode) {
    case COLUMN_TYPE_24:
        szPrinterCommand[0] = ESC;
        strcpy(&szPrinterCommand[1], "F24;");
        break;
    case COLUMN_TYPE_32:
        szPrinterCommand[0] = ESC;
        strcpy(&szPrinterCommand[1], "F32;");
        break;
    case COLUMN_TYPE_42:
        szPrinterCommand[0] = ESC;
        strcpy(&szPrinterCommand[1], "F42;");
        break;
    }

    write(obPrinter.inHandle, szPrinterCommand, 5);
} /* End of p350_Col_Mode () */

void p350LinesPerInch(int lpi) {
    char szPrinterCommand[6];

    szPrinterCommand[0] = ESC;

    if (lpi == 9)
        strcpy(&szPrinterCommand[1], "a20;");
    else if (lpi == 10)
        strcpy(&szPrinterCommand[1], "a18;");
    else
        strcpy(&szPrinterCommand[1], "a22;"); /* default to 8 lpi */

    write(obPrinter.inHandle, szPrinterCommand, 5);
    szPrinterCommand[1] = 'w';
    szPrinterCommand[2] = '3';
    szPrinterCommand[3] = ';';
    write(obPrinter.inHandle, szPrinterCommand, 4);
} /* End of p350LinesPerInch () */

int inPrintFormFeedsOperation(TRANSACTION_OBJECT* pobTran) {
    pdebug(("--inPrintFormFeedsOperation--"));

    if (srGDS.fSoftFormFeedKey) {
        if (inChkPrinterStatus(&obPrinter) == VS_SUCCESS)
            inPrintLines(&obPrinter, LINEFEEDS_START, FOOTER_END, NO_BL_LINE, 0L);
    }

    return(BRANCH_EXIT);
}
#ifdef PRINTISO_DEFINED
int inPrintISOPacket(VS_BOOL fSendPacket, unsigned char *pucMessage, int inLen)///ivan

{

    extern PRINTER_OBJECT obPrinter;

    char ucLineBuffer[24 + 1 + 1];

    unsigned char *pucBuff;

    int inBuffPtr = 0;




      if (inLen <= 0)

          return(VS_SUCCESS);

      p350ColumnMode(COLUMN_TYPE_24);

      if (fSendPacket)

      {

          shSpool(&obPrinter, "Send ISO Packet\n");
	   LOG_PRINTFF((0X8l,"Send ISO Packet"));  	  

      }

      else

      {

          shSpool(&obPrinter, "Receive ISO Packet\n");
	   LOG_PRINTFF((0X8l,"Receive ISO Packet"));   

      }



      shSpool(&obPrinter, "\n----\n");

      pucBuff = pucMessage + inLen;

      while (pucBuff > pucMessage)

      {

          MEMCLR(ucLineBuffer, sizeof(ucLineBuffer));

          for (inBuffPtr = 0; (inBuffPtr < 24) && (pucBuff > pucMessage); inBuffPtr += 3)

          {



              sprintf(&ucLineBuffer[inBuffPtr], "%02X ", *pucMessage);

              pucMessage++;

          }

          ucLineBuffer[24] = '\n';

          shSpool(&obPrinter, ucLineBuffer);
	   LOG_PRINTFF((0X8l,"%s",ucLineBuffer));  
		  

      }

      shSpool(&obPrinter, "\n----\n\n\n");


	p350ColumnMode (inGetP350ColMode() );

	  return (VS_SUCCESS);

}
#endif /* PRINTISO_DEFINED */

#undef VSPRINT_C
